
package com.mrd.gtimpframe.strategy;

import java.util.ArrayList;
import java.util.List;

/**
 * 消息公告-用户角色匹配公告
 */
public class NoticeRoleMatcher {
    public enum NoticeRole {
        ALL(
                "admin", //admin
                "402880bd3c09d29e013c09d338400003"  //平台管理员
        ),
        CUSTOMER(
                "4028803e5262fa340152632f9b2d0004", //公司管理
                "402880bd3c09d29e013c09d338400001", //客户
                "f0fc943258f1574c0158f24ffd8900d9", //公司管理-3.0
                "f0fc943258f1574c0158f24df35700ce"  //客户-3.0
        ),
        SUPPLIER(
                "f0fc943258f1574c0158f24e4f5f00cf", //报关公司管理-3.0
                "402881ea5303ec8f0153044429e40005", //报关公司管理
                "402880bd3c09d29e013c09d2c6d80000", //报关供应商管理员
                "f0fc943258f1574c0158f24da7aa00cd"  //报关供应商管理员-3.0
        );
        private String[] roleIdArr;
        NoticeRole(String... roleIdArr){
            this.roleIdArr = roleIdArr;
        }

        public String[] getRoleIdArr() {
            return roleIdArr;
        }

        public static List<String> allName(){
            List<String> names = new ArrayList<>();
            for (NoticeRole noticeRole : values()) {
                names.add(noticeRole.name());
            }
            return names;
        }
    }

    public static boolean doMatch(String code, List<String> roleIds){
        if(code == null || code.trim().length() == 0){
            return false;
        }
        if(NoticeRole.ALL.name().equals(code)){
            return true;
        }
        NoticeRole noticeRole = null;
        try {
            noticeRole = NoticeRole.valueOf(code);
        }catch (IllegalArgumentException e){
            e.printStackTrace();
            return false;
        }
        String[] validRoleIds = noticeRole.getRoleIdArr();
        if(validRoleIds == null){
            return false;
        }
        for (String validRoleId : validRoleIds) {
            for (String roleId : roleIds) {
                if (roleId.equals(validRoleId)) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean matchAdmin(List<String> roleIds){
        if(roleIds == null || roleIds.size() == 0){
            return false;
        }
        for (String validRoleId : NoticeRole.ALL.getRoleIdArr()) {
            for (String roleId : roleIds) {
                if (roleId.equals(validRoleId)) {
                    return true;
                }
            }
        }
        return false;
    }
}
